package com.gxuwz.lhleavesys.web.servlet;

import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import com.gxuwz.lhleavesys.context.*;
import com.gxuwz.lhleavesys.entity.Classes;
import com.gxuwz.lhleavesys.entity.Course;
import com.gxuwz.lhleavesys.entity.Teacher;
import com.gxuwz.lhleavesys.service.ClassesService;
import com.gxuwz.lhleavesys.service.CourseService;
import com.gxuwz.lhleavesys.service.impl.ClassesServiceImpl;
import com.gxuwz.lhleavesys.service.impl.CourseServiceImpl;;

@WebServlet("/CourseServlet")
public class CourseServlet extends BaseLHServlet {

    private String view_courses = "/views/teacher/courseList.jsp";
    private String view_edit = "/views/teacher/editview.jsp";
    private String view_add = "/views/teacher/addCourse.jsp";

    @Autowired
    private CourseService courseService;

    @Autowired
    private ClassesService classesService;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            this.courseService = (CourseServiceImpl) AppContext.getBean("courseServiceImpl", CourseServiceImpl.class);
            this.classesService = (ClassesServiceImpl) AppContext.getBean("classesServiceImpl",
                    ClassesServiceImpl.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if (action == null) {// 二级学院首页,包括查询所有或一个
            request.getParameter("courseID");
            request.getParameter("courseName");
            request.getParameter("term");
            List<Course> courses = courseService.getQueryAllCourses(request.getParameter("courseID"),
                    request.getParameter("courseName"), request.getParameter("term"));
            request.setAttribute("courses", courses);
            request.getRequestDispatcher(view_courses).forward(request, response);
        } else if (action.equals("add")) {// 新增
            Course course = new Course();
            course.setCourseID(request.getParameter("courseID"));
            course.setTerm(request.getParameter("term"));
            course.setClassID(request.getParameter("classID"));
            course.setCourseName(request.getParameter("courseName"));
            course.setYear(request.getParameter("year"));
            course.setHour(Integer.valueOf(request.getParameter("hour")));
            boolean flag = courseService.addCourse(course);
            if (flag) {
                doRedirect(request, response, "/CourseServlet");
            }
        } else if (action.equals("delete")) {// 删除
            String ID = request.getParameter("courseID");
            boolean flag = courseService.deleteCourse(ID);
            if (flag) {
                doRedirect(request, response, "/CourseServlet");
            }
        } else if (action.equals("edit")) {// 修改
            String id = request.getParameter("id");// 前端传过来的课程id
            Course course = new Course();
            course.setCourseID(id);
            course.setCourseName(request.getParameter("courseName"));
            boolean flag = courseService.updateCourse(course);
            if (flag) {
                doRedirect(request, response, "/CourseServlet");
            }
        } else if (action.equals("viewadd")) {// 新增课程页面
            List<Classes> classesList = classesService.getAllClasses();
            request.setAttribute("classesList", classesList);
            doDispatch(request, response, view_add);
        } else if (action.equals("viewedit")) {// 修改课程页面
            String ID = request.getParameter("id");// 前端传过来的课程id
            Course course = courseService.getCourseByCourseID(ID);
            List<Classes> classesList = classesService.getAllClasses();
            System.out.println(course.getClassID());
            Classes classes = classesService.getClassByClassID(course.getClassID());
            request.setAttribute("classesList", classesList);
            request.setAttribute("classes", classes);
            request.setAttribute("course", course);
            doDispatch(request, response, view_edit);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }
}
